import {vec3} from "gl-matrix";
import {Scene} from "../core/scene";


export class Ray {

    static tmpVec3 = vec3.create();

    constructor(public origin:vec3,public direction:vec3){
        vec3.normalize(direction,direction);
    }

    distanceToPoint(point:vec3){
        vec3.sub(Ray.tmpVec3,point,this.origin);
        let dirDistance = vec3.dot(Ray.tmpVec3,this.direction);
        if (dirDistance < 0){
            return vec3.len(vec3.sub(Ray.tmpVec3,this.origin,point));
        }
        // 求出垂足
        vec3.scale(Ray.tmpVec3,this.direction,dirDistance);
        vec3.add(Ray.tmpVec3,this.origin,Ray.tmpVec3);

        vec3.sub(Ray.tmpVec3,Ray.tmpVec3,point);
        return vec3.len(Ray.tmpVec3);
    }

}
